﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

using System.Drawing;

public partial class ChangeInformation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ChangeInfor();
            SetTop(0);
        }
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        String sid = Session["mid"].ToString();
        ds = db.GetDataSet("select * from mr_v_comment where time > (select last_comment_time from mr_member where mid ='" + sid + "')");
        HyperLinkRecentComment.Text = "最新评论(" + ds.Tables[0].Rows.Count.ToString() + ")";
        HyperLinkRecentComment.NavigateUrl = "~/recentcomment.aspx";
        HyperLinkRecentComment.ForeColor = Color.Blue;
        ds = db.GetDataSet("select * from mr_v_download where time > (select last_download_time from mr_member where mid ='" + sid + "')");
        HyperLinkRecentDownload.Text = "最新下载(" + ds.Tables[0].Rows.Count.ToString() + ")";
        HyperLinkRecentDownload.NavigateUrl = "~/recentdownload.aspx";
        HyperLinkRecentDownload.ForeColor = Color.Blue;
    }
    protected void ChangeInfor()
    {
        Session["mid"] = 2;
        DBaccess db = new DBaccess();
        string sql = "select mname,dname,gpa,score from mr_member a,mr_department b,mr_student c where a.mid=c.sid and c.department= b.did and a.mid='"+Session["mid"].ToString()+"'";
        SqlDataReader read = db.GetReader(sql);
        read.Read();
        TextBox1.Text = read.GetValue(0).ToString();
        Label3.Text = read.GetValue(1).ToString();
        Label5.Text = read.GetValue(2).ToString();
        Label7.Text = read.GetValue(3).ToString();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("../ChangeInformation.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Session["mid"] = 1;
        DBaccess db = new DBaccess();
        string name = TextBox1.Text;
        if (FileUpload1.HasFile == true)
        {
            string kzm1 = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
            //string path = FileUpload1.PostedFile.FileName;
            string path1 = "Picture\\user\\" + Session["mid"].ToString() + kzm1;
            string fullpath1 = MapPath(".") + "\\" + path1;
            string resourcepath1 = "~Picture\\user\\" + Session["mid"].ToString() + kzm1;
            FileUpload1.PostedFile.SaveAs(fullpath1);
            System.Drawing.Image image = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream);

            int height = image.Height;
            int width = image.Width;
            if (height == 64 && width == 64)
            {
                string sql = "update mr_member set mname='" + name + "',logo='" + resourcepath1 + "' where mid='"+Session["mid"].ToString()+"'";//数据库操作
                if (db.ExecSql(sql) != 0)
                {
                    Response.Write("<script>alert('修改成功!');</script>");
                    TextBox1.Text = name;
                }
            }
            else
                Response.Write("<script>alert('请上传大小为64*64大小的图片');</script>");
        }
    }
    public void SetTop(int tag)
    {
        DBaccess db = new DBaccess();
        DataSet ds = new DataSet();
        String tablename = "mr_test";
        String condition = "";
        DateTime currentTime = new DateTime();
        currentTime = DateTime.Now;
        if (tag == 0)
        {
            tablename = "mr_v_downloadcount";
            LabelTitle.Text = "总排行榜";
        }
        else if (tag == 1)
        {
            tablename = "mr_v_downloadcount_month ";
            condition = "where year = '" + currentTime.Year.ToString() + "' and month = '" + (currentTime.Month - 1).ToString() + "'";
            LabelTitle.Text = "月排行榜";
        }
        else if (tag == 2)
        {
            tablename = "mr_v_downloadcount_year ";
            condition = "where year = '" + currentTime.Year.ToString() + "'";
            LabelTitle.Text = "年排行榜";
        }
        ds = db.GetDataSet("select top 5 * from " + tablename + condition + "  order by num desc");
        HyperLinkRank1.Text = ds.Tables[0].Rows[0]["rname"].ToString();
        HyperLinkRank1.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount1.Text = ds.Tables[0].Rows[0]["num"].ToString();
        HyperLinkRank2.Text = ds.Tables[0].Rows[1]["rname"].ToString();
        HyperLinkRank2.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount2.Text = ds.Tables[0].Rows[1]["num"].ToString();
        HyperLinkRank3.Text = ds.Tables[0].Rows[2]["rname"].ToString();
        HyperLinkRank3.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount3.Text = ds.Tables[0].Rows[2]["num"].ToString();
        HyperLinkRank4.Text = ds.Tables[0].Rows[3]["rname"].ToString();
        HyperLinkRank4.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount4.Text = ds.Tables[0].Rows[3]["num"].ToString();
        HyperLinkRank5.Text = ds.Tables[0].Rows[4]["rname"].ToString();
        HyperLinkRank5.NavigateUrl = "~/comment.aspx?rid=" + ds.Tables[0].Rows[0]["rid"].ToString();
        LabelCount5.Text = ds.Tables[0].Rows[4]["num"].ToString();
    }
    protected void ButtonMonth_Click(object sender, EventArgs e)
    {
        SetTop(1);
    }
    protected void ButtonYear_Click(object sender, EventArgs e)
    {
        SetTop(2);
    }
    protected void ButtonAll_Click1(object sender, EventArgs e)
    {
        SetTop(0);
    }
}